package com.ycbj.android.app;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;

import com.ycbj.android.app.fragment.HomeFragment;
import com.ycbj.android.app.fragment.MeFragment;
import com.ycbj.android.app.manager.MainUiManager;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private List<Fragment> mFragments = new ArrayList<>();
//    private FrameLayout mFrameLayout_container;  mianactivity中负责占位的帧布局
    private FrameLayout layout_home, layout_mid_iv, layout_me;
    HomeFragment mHomeFragment = new HomeFragment();
    MeFragment mMeFragment = new MeFragment();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }
    private void initView() {
        mFragments.add(mHomeFragment);
        mFragments.add(mMeFragment);

       // mFrameLayout_container = (FrameLayout) findViewById(R.id.main_fragment_container);

        layout_home = ((FrameLayout) findViewById(R.id.frame_home));
        layout_mid_iv= ((FrameLayout) findViewById(R.id.frame_home_mid));
        layout_me = ((FrameLayout) findViewById(R.id.frame_me));


        layout_home.setOnClickListener(this);
        layout_mid_iv.setOnClickListener(this);
        layout_me.setOnClickListener(this);

        //替换占位的fragment
        replaceFragment(mHomeFragment);

    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //TODO 这里替换的是main_fragment_container 主界面中的预留fragment
        // or fragment_base_child_container  BaseFargent中给子类预留的fragment
        transaction.replace(R.id.main_fragment_container, fragment);
        transaction.commit();
        //
        MainUiManager.getInstance().replaceFragment(fragment,this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.frame_home:
                replaceFragment(mHomeFragment);

                break;
            case R.id.frame_me:
                replaceFragment(mMeFragment);

                break;
            case R.id.frame_home_mid:
                //这里点击中间就是开启另一个activity
                //中间凸起的部分不是fragment是一个新的界面
                //startActivity();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.initClient(new OkHttpClient()).cancelTag(this);
    }
}